package com.shqcjd.preinstallmobile.ui.fragment;

import android.graphics.Paint;
import android.support.design.widget.TabLayout;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.view.animation.LinearOutSlowInInterpolator;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.shqcjd.preinstallmobile.R;
import com.shqcjd.preinstallmobile.model.eventbus.FinishActivityEvent;
import com.shqcjd.preinstallmobile.ui.adapter.TabLayoutViewPagerAdapter;
import com.shqcjd.preinstallmobile.ui.base.BaseFragment;
import com.shqcjd.preinstallmobile.ui.base.BasePresenter;
import com.shqcjd.preinstallmobile.ui.fragment.mine.HistoryInspectionFrament;
import com.shqcjd.preinstallmobile.ui.fragment.mine.RemoteGuideHistoryFragment;
import com.shqcjd.preinstallmobile.ui.fragment.mine.WaitInspectionFrament;
import com.shqcjd.preinstallmobile.util.PrefUtils;
import com.shqcjd.preinstallmobile.util.UIUtils;
import com.shqcjd.preinstallmobile.widget.WaveView;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 我的
 * Created by sy on 2019/3/14.
 */

public class MineFragment extends BaseFragment {

    @BindView(R.id.wv_icon_bg)
    WaveView wvIconBg;
    @BindView(R.id.civ_icon)
    CircleImageView civIcon;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_position_type)
    TextView tvPositionType;
    @BindView(R.id.tab_mine_layout)
    TabLayout tab_mine_layout;
    @BindView(R.id.viewpager_mine)
    ViewPager viewpager_mine;
    @BindView(R.id.ib_quit)
    ImageButton ibQuit;

    public static MineFragment newInstance() {
        return new MineFragment();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    public void initView(View rootView) {
        initUser();
        initPhoto();
        initViewPager();


    }

    private void initUser() {
        String userName = PrefUtils.getString(getActivity(), PrefUtils.USER_NAME, "");
        String userIcon = PrefUtils.getString(getActivity(), PrefUtils.USER_ICON, "");
        int usertype = PrefUtils.getInt(getActivity(), PrefUtils.USER_TYPE, -1);

        tvName.setText(userName);
        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.ic_default_tx)    //加载成功之前占位图
                .error(R.mipmap.ic_default_tx) ;   //加载错误之后的错误图
//                .override(57, 57);//指定图片的缩放类型为centerCrop （等比例缩放图片，直到图片的宽高都
        Glide.with(getContext())
                .load(userIcon)
                .apply(options)
                .into(civIcon);
        if (usertype == 1) {//1巡检员 2专家
            tvPositionType.setText("巡检员");
        } else {
            tvPositionType.setText("专家");
        }


    }

    private void initViewPager() {
        //
        TabLayoutViewPagerAdapter adapter = new TabLayoutViewPagerAdapter(getChildFragmentManager());
        adapter.addFragment(WaitInspectionFrament.newInstance(), "待巡检");
        adapter.addFragment(HistoryInspectionFrament.newInstance(), "已巡检");
        adapter.addFragment(RemoteGuideHistoryFragment.newInstance(), "远程指导");
        viewpager_mine.setAdapter(adapter);
        tab_mine_layout.setupWithViewPager(viewpager_mine);
    }

    private void initPhoto() {
        //水波纹效果
        wvIconBg.setDuration(5000);
        wvIconBg.setStyle(Paint.Style.FILL_AND_STROKE);
        wvIconBg.setColor(ContextCompat.getColor(getContext(), R.color.white));
        wvIconBg.setInterpolator(new LinearOutSlowInInterpolator());
        wvIconBg.setMaxRadius(UIUtils.dp2px(50));
        wvIconBg.start();
    }


    @OnClick(R.id.ib_quit)
    public void onViewClicked() {
        EventBus.getDefault().post(new FinishActivityEvent());
    }



}
